//
//  PhotoBrowserCollectionViewCell.swift
//  JKWeiBo
//
//  Created by 王冲 on 2018/1/4.
//  Copyright © 2018年 希爱欧科技有限公司. All rights reserved.
//

import UIKit
import SDWebImage

// MARK: 1、定义一个协议
protocol PhotoBrowserCollectionViewCellDelegate: NSObjectProtocol
{
    func photoBrowserClose(cell: PhotoBrowserCollectionViewCell)
}

class PhotoBrowserCollectionViewCell: UICollectionViewCell {
    
    // 2.定义协议的变量
    weak var photoBrowserDelegate: PhotoBrowserCollectionViewCellDelegate?
    
    var imageURL: NSURL?{
        didSet{
            
            // 1.重置属性
            reset()
            
            // 2.显示菊花
            activity.startAnimating()
            
            // 3.设置图片
            iconView.sd_setImage(with: imageURL! as URL) { (image, _, _, _) in
                
                // 4.隐藏菊花
                self.activity.stopAnimating()
                
                // 5.调整图片的位置和尺寸
                self.setImageViewPosition()
                
            }
            //iconView.sd_setImage(with: imageURL! as URL)
        }
    }
    // MARK: 重置scrollview和imgeView的属性
    func reset(){
        
        // 重置scrollview
        scrollview.contentSize = CGSize.zero
        scrollview.contentInset = UIEdgeInsets.zero
        scrollview.contentOffset = CGPoint.zero
        
        // 重置imageview
        iconView.transform = CGAffineTransform.identity
    }
    
    // MARK: 图片尺寸的重新计算
    func setImageViewPosition(){
        
        /*
         *  1.图片显示不完整
         *  2.图片没有居中显示
         *  3.长图显示也会有一些问题
         */
        let size = self.disPlaySize(image: iconView.image!)
        self.iconView.frame = CGRect(origin: CGPoint.zero, size: size)
        // 1.拿到按照宽高比计算之后的图片大小
        // 2.判断图片的高度是否大于屏幕的高度
        if size.height > JKscreenH
        {
            // 3.1.大于 长图 -> y等于0 设置scrollview的滚动范围为图片的大小
            scrollview.contentSize = size
            
        }else{
            
            // 3.2.小于 小图 -> 设置图片居中
            let y = (JKscreenH-size.height)*0.5
            scrollview.contentInset = UIEdgeInsets(top: y, left: 0, bottom: y, right: 0)
        }
    }
    
    // MARK: 计算图片的宽高比
    private func disPlaySize(image: UIImage) -> CGSize{
    
        
      // 1.拿到图片的宽高比
        let scale = image.size.height/image.size.width
      // 2.根据宽高比计算高度
        let width  = JKscreenW
        let height = width*scale
        return CGSize(width: width, height: height)
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        contentView.addSubview(scrollview)
        scrollview.addSubview(iconView)
        contentView.addSubview(activity)
        
        activity.center = contentView.center
        
        scrollview.delegate = self
        scrollview.maximumZoomScale = 2.0
        scrollview.minimumZoomScale = 0.5
        
        let tap = UITapGestureRecognizer(target: self, action: #selector(PhotoBrowserCollectionViewCell.close))
        iconView.addGestureRecognizer(tap)
        
    }
    
    // MARK: 点击图片单击返回
    func close(){
        
        photoBrowserDelegate?.photoBrowserClose(cell: self)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    // MARK: 添加scrollView
    lazy var scrollview:UIScrollView = {
        
        let scrollView = UIScrollView(frame: CGRect(x: 0, y: 0, width: JKscreenW, height: JKscreenH))
        scrollView.showsVerticalScrollIndicator = true
        return scrollView
    }()
    
    // MARK: 添加图片
    lazy var iconView: UIImageView = {
        
        let iconImage = UIImageView()
        iconImage.isUserInteractionEnabled = true
        return iconImage
    }()
    
    private lazy var activity: UIActivityIndicatorView =  UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.whiteLarge)
}

extension PhotoBrowserCollectionViewCell: UIScrollViewDelegate{
    
    // 告诉系统要缩放哪个控件
    func viewForZooming(in scrollView: UIScrollView) -> UIView? {
        
        return iconView
    }
    
    // 缩放结束后重新调整图片的位置
    // view 被缩放的视图
    func scrollViewDidEndZooming(_ scrollView: UIScrollView, with view: UIView?, atScale scale: CGFloat) {
        
        // 注意: 缩放的本质是修改transform,而修改transform不会影响到bouns,只有frame会受到影响
        print("scrollViewDidEndZooming")
        
        var offSetX = (JKscreenW-view!.width)*0.5
        var offSetY = (JKscreenH-view!.height)*0.5
        
        offSetX = offSetX < 0 ? 0:offSetX
        offSetY = offSetY < 0 ? 0:offSetY
        
        scrollView.contentInset = UIEdgeInsets(top: offSetY, left: offSetX, bottom: offSetY, right: offSetX)
    }
    
}

